define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clothmaking_raw/index/index' + location.search,
                    add_url: 'clothmaking_raw/index/add',
                    edit_url: 'clothmaking_raw/index/edit',
                    del_url: 'clothmaking_raw/index/del',
                    multi_url: 'clothmaking_raw/index/multi',
                    import_url: 'clothmaking_raw/index/import',
                    table: 'clothmaking_raw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'name', title: __('Name'), operate: 'LIKE' },
                        { field: 'product_surplus', title: __('Product_surplus') },
                        { field: 'stir_surplus', title: __('Stir_surplus') },
                        { field: 'merge_surplus', title: __('Merge_surplus') },
                        { field: 'merge_time', title: __('Merge_time'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'user_id', title: __('User_id') ,formatter(value,row,index){
                            if(row['userinfo']!=null)
                            {
                                 return row['userinfo'].username;   
                            }
                            return;
                        }},
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        // { field: 'customformid', title: __('Customformid') },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            // buttons: [
                            //     {
                            //         name: 'addsub',
                            //         title: '原材料配比',
                            //         classname: 'btn btn-warning btn-xs btn-dialog btn-addsub',
                            //         icon: 'fa fa-plus',

                            //     },
                            //     {
                            //         name: 'view',
                            //         title: '查看',
                            //         classname: 'btn btn-info btn-xs btn-dialog',
                            //         icon: 'fa fa-eye',
                            //         url: 'clothmaking_raw/index/view/id/{ids}'
                            //     }
                            // ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            table.on('click', '.btn-addsub', function(e) {
                e.stopPropagation();

                var options = table.bootstrapTable('getOptions');

                var index = $(this).closest('tr').data('index');

                var data = table.bootstrapTable('getData');

                var cfid = data[index]['customformid'];
                var id = data[index][options.pk]

                Fast.api.open('formdesign/foreign/add?form_id=' + cfid + '&aid=' + id + '&t=raw&only=1', '原材料配比');

            });


        },
        add: function() {
            Controller.api.bindevent();
        },
        edit: function() {
            Controller.api.bindevent();
        },
        view: function() {

            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});